const mysql = require('mysql2/promise');
const CONFIG = require('./config');

async function createDatabase() {
    try {
        // 首先创建连接（不指定数据库）
        const connection = await mysql.createConnection({
            host: CONFIG.DB.host,
            user: CONFIG.DB.user,
            password: CONFIG.DB.password
        });

        console.log('连接到MySQL服务器...');

        // 创建数据库
        await connection.execute(`CREATE DATABASE IF NOT EXISTS ${CONFIG.DB.database}`);
        console.log(`数据库 ${CONFIG.DB.database} 创建成功`);

        await connection.end();

        // 现在初始化数据库表
        const { initializeDatabase } = require('./database');
        await initializeDatabase();
        
        console.log('数据库初始化完成！');
        process.exit(0);
    } catch (error) {
        console.error('数据库初始化失败:', error);
        process.exit(1);
    }
}

// 运行初始化
createDatabase();